package com.formiaomiao.demain.activity;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.formiaomiao.demain.R;
import com.formiaomiao.demain.base.BaseActivity;
import com.formiaomiao.demain.base.BaseUrl;
import com.formiaomiao.demain.model.UserBean;
import com.formiaomiao.demain.util.RegexUtils;
import com.formiaomiao.demain.util.TimeCount;
import com.formiaomiao.demain.util.ToastUtils;
import com.formiaomiao.demain.weight.LoadingDialog;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.AbsCallback;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.HttpParams;
import com.lzy.okgo.model.Response;

import java.util.UUID;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 注册页面
 */
public class RegisterActivity extends BaseActivity {

    @BindView(R.id.iv_class_back)
    ImageView ivClassBack;
    @BindView(R.id.tv_class_name)
    TextView tvClassName;
    @BindView(R.id.et_register_user)
    EditText etRegisterUser;
    @BindView(R.id.et_register_pwd)
    EditText etRegisterPwd;
    @BindView(R.id.et_register_pwd2)
    EditText etRegisterPwd2;
    @BindView(R.id.et_register_verification)
    EditText etRegisterVerification;
    @BindView(R.id.tv_register_verification)
    TextView tvRegisterVerification;
    @BindView(R.id.btn_login)
    Button btnLogin;

    TimeCount timeCount;

    LoadingDialog loadingDialog;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);
        ButterKnife.bind(this);
        ViewInit();
    }

    public void ViewInit() {
        tvClassName.setText("注册");
        timeCount = new TimeCount(60000, 1000, tvRegisterVerification);
        loadingDialog = new LoadingDialog(this);
    }

    protected void onStop() {
        super.onStop();
    }

    /**
     * 注册网络操作
     *
     * @param username
     * @param password
     */
    public void RegisterInit(String username, String password, String code) {
        loadingDialog.show();
        HttpParams params = new HttpParams();
        params.put("userName",username);
        params.put("passWord",password);
        params.put("userPhone",username);
        params.put("nickName","demain_"+username);
        params.put("validateCode",code);
        OkGo.<String>post(BaseUrl.USERREGISTER).params(params).tag(this).execute(new StringCallback() {
            @Override
            public void onSuccess(Response<String> response) {
                loadingDialog.dismiss();
                if(response.body().contains("200")){
                    ToastUtils.show(RegisterActivity.this,"注册成功，请登录");
                    finish();
                }else{
                    ToastUtils.show(RegisterActivity.this,"注册失败");
                }
            }

            @Override
            public void onError(Response<String> response) {
                super.onError(response);
                loadingDialog.dismiss();
            }
        });
    }

    /**
     * 发送验证码
     */
    public void SendSMS(String username) {
        loadingDialog.show();
        HttpParams params = new HttpParams();
        params.put("phone",username);
        OkGo.<String>post(BaseUrl.USERSENDSMSMESSAGE).params(params).tag(this).execute(new StringCallback() {
            @Override
            public void onSuccess(Response<String> response) {
                loadingDialog.dismiss();
                if(response.body().contains("200")){
                    ToastUtils.show(RegisterActivity.this,"验证码发送成功，请注意查收");
                }else{
                    ToastUtils.show(RegisterActivity.this,"验证码发送失败");
                }
            }

            @Override
            public void onError(Response<String> response) {
                super.onError(response);
                loadingDialog.dismiss();
            }
        });
    }

    @OnClick({R.id.iv_class_back, R.id.tv_register_verification, R.id.btn_login})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_class_back:
                finish();
                break;
            case R.id.tv_register_verification:
                if (tvRegisterVerification.getText().toString().equals("获取验证码")) {
                    if (etRegisterUser.getText().toString().trim().equals("")) {
                        Toast.makeText(this, "请输入手机号", Toast.LENGTH_SHORT).show();
                    } else {
                        if (RegexUtils.checkPhone(etRegisterUser.getText().toString().trim())) {
                            SendSMS(etRegisterUser.getText().toString());
                        } else {
                            Toast.makeText(this, "请输入正确的手机号", Toast.LENGTH_SHORT).show();
                        }
                    }
                } else {
                    Toast.makeText(this, "请稍后...", Toast.LENGTH_SHORT).show();
                }
                break;
            case R.id.btn_login:
                if (etRegisterUser.getText().toString().trim().equals("")) {
                    Toast.makeText(this, "请输入手机号", Toast.LENGTH_SHORT).show();
                } else if (etRegisterPwd.getText().toString().trim().length() < 6) {
                    Toast.makeText(this, "密码长度最少为8位", Toast.LENGTH_SHORT).show();
                } else if (etRegisterPwd.getText().toString().trim().equals("")) {
                    Toast.makeText(this, "请输入密码", Toast.LENGTH_SHORT).show();
                } else if (etRegisterPwd2.getText().toString().trim().equals("")) {
                    Toast.makeText(this, "请再输入一次密码", Toast.LENGTH_SHORT).show();
                } else if (etRegisterVerification.getText().toString().trim().equals("")) {
                    Toast.makeText(this, "请输入验证码", Toast.LENGTH_SHORT).show();
                } else if (!etRegisterPwd2.getText().toString().trim().equals(etRegisterPwd.getText().toString().trim())) {
                    Toast.makeText(this, "两次输入的密码不一致", Toast.LENGTH_SHORT).show();
                } else {
                    RegisterInit(etRegisterUser.getText().toString(),etRegisterPwd.getText().toString(),etRegisterVerification.getText().toString());
                }
                break;
        }
    }
}
